package cn.xzq.crm.action;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.xzq.crm.entity.Customer;
import cn.xzq.crm.entity.LinkMan;
import cn.xzq.crm.service.CustomerService;
import cn.xzq.crm.service.LinkManService;

@Controller("linkManAction")
@Scope("prototype")
public class LinkManAction extends ActionSupport implements ModelDriven<LinkMan>{
	
	@Autowired
	private LinkManService linkManService;
	
	@Autowired
	private CustomerService customerService;
	
	//模型驱动封装
	private LinkMan linkMan = new LinkMan();
	public LinkMan getModel() {
		return linkMan;
	}
	public LinkMan getLinkMan() {
		return linkMan;
	}

	//customers值栈
	private List<Customer> customers;
	public List<Customer> getCustomers() {
		return customers;
	}
	//LinkMans值栈
	private  List<LinkMan> LinkMans;
	public List<LinkMan> getLinkMans() {
		return LinkMans;
	}
	/*
	 * 需要上传文件（流）
	 * 需要上传文件名
	 * 1、在action里面成员位置定义成员变量
	 * 		- 一个表示上传文件
	 * 		- 一个表示文件名
	 * 2、生成get/set方法()
	 * 3、还有一个变量mime不需要设置，struts2封装好了
	 */
	private File upload; //与表单文件上传项name值一致
	private String uploadFileName; //名字固定：File变量名+FileName
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	//==================================
	/**
	 * 跳转到添加页面
	 * @return
	 */
	public String toAddPage(){
		//获取客户列表，保存到值栈中
		customers = customerService.findAll();
		//System.out.println("long:"+customers.size());
		return "toAddPage";
	}
	
	/**
	 * 跳转编辑页面
	 * @return
	 */
	public String toEditPage(){
		//获取客户列表,，保存到值栈中
		customers = customerService.findAll();
		linkMan = linkManService.findOneById(linkMan.getLkmId());
		return "toEditPage";
	}
	
	/**
	 * 跳转到列表页面
	 * @return
	 */
	public String toListPage(){
		//获取联系人列表，保存到值栈中
		LinkMans = linkManService.findAll();
		//System.out.println("long:"+customers.size());
		return "toListPage";
	}
	
	/**
	 * 跳转到综合查询页面
	 * @return
	 */
	public String toQueryPage(){
		//获取客户列表,，保存到值栈中
		customers = customerService.findAll();
		return "toQueryPage";
	}
	
	/**
	 * 添加联系人
	 * @return
	 * @throws IOException 
	 */
	public String addLinkMan() throws IOException {
		/*
		 * 文件上传模块
		 */
		if(upload != null){
			//写上传代码
			//在服务器文件夹里面创建文件
			File serviceFile = new File("D:/Tomcat/testUpload/"+uploadFileName);
			//把上传文件复制到服务器里面
			FileUtils.copyFile(upload, serviceFile);
		}
		
		//找到customer
		Customer customer = customerService.findOneById(linkMan.getCustomer().getCustId());
		//封装customer到linkman中
		linkMan.setCustomer(customer);
		//添加到数据库
		linkManService.add(linkMan);
		return "addLinkMan";
	}
	
	/**
	 * 修改联系人
	 * @return
	 */
	public String update() {
		//找到customer
		Customer customer = customerService.findOneById(linkMan.getCustomer().getCustId());
		//封装customer到linkman中
		linkMan.setCustomer(customer);
		//添加到数据库
		//System.out.println(linkMan);
		linkManService.update(linkMan);
		return "update";
	}
	
	/**
	 * 删除一个联系人
	 * @return
	 */
	public String delete() {
		//找到根据id 查找linkman
		linkMan = linkManService.findOneById(linkMan.getLkmId());
		//删除数据
		linkManService.delete(linkMan);
		return "delete";
	}
	
	/**
	 * 多条件组合查询
	 * @return
	 */
	public String moreCondition(){
		//找到customer
		Customer customer = customerService.findOneById(linkMan.getCustomer().getCustId());
		//封装customer到linkman中
		linkMan.setCustomer(customer);
		System.out.println(linkMan);
		LinkMans = linkManService.findMoreCondition(linkMan);
		customers = customerService.findAll();
		return "moreCondition";
	}
	
}
